home *** CD-ROM | disk | FTP | other *** search
/ PsL Monthly 1993 December / PSL Monthly Shareware CD-ROM (December 1993).iso / prgmming / dos / c / hsend.exe / HSEND.C < prev    next >
Text File  |  1992-01-11  |  26KB  |  1,066 lines

  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <conio.h>
  4. #include <dir.h>
  5. #include <dos.h>
  6. #include <time.h>
  7. #include <async.h>
  8. #include <process.h>
  9. #include "hsend.h"
  10.  
  11. int WAITING,BAUD,COMM,RINGS,SCREENFLAG,LOGFILEFLAG,FLAGFILEFLAG;
  12. int HARDERR_FLAG=0,TIMEOUT=180;
  13. char LOGFILE[61],FLAGFILE[61],PASSWORD[13];
  14. ASYNC *P=NULL;
  15.  
  16.  
  17. main(argc,argv)
  18. int argc;
  19. char argv[];
  20. {
  21. ctrlbrk(break_handler);
  22. harderr(hardware_error_handler);
  23. load_data();
  24.  
  25. if(argc>2){
  26.     cprintf("\n\rUsage:  hsend <no arguments>    Run in host mode\n\r");
  27.     cprintf("        hsend <batch file name> Run in guest mode\n\r");
  28.     beep(3,400);
  29.     exit(1);}
  30.  
  31. copyright();
  32.  
  33. if(!init_modem()){
  34.     gotoxy(1,21);
  35.     textcolor(LIGHTGRAY);
  36.     cprintf("\n\rCOMM device did not respond.  Check settings with CONFIGUR.\n\r");
  37.     beep(3,400);
  38.     exit(1);}
  39.  
  40. clrscr();
  41. textcolor(LIGHTCYAN);
  42. empty_box(1,1,80,20);
  43. empty_box(1,21,80,24);
  44.  
  45. if(argc==2) guest_mode(argv[2]);
  46.  else host_mode();
  47.  
  48. window(1,1,80,25);
  49. gotoxy(3,22);
  50. textcolor(LIGHTMAGENTA);
  51. cprintf("Initializing modem, please wait...");
  52. init_modem();
  53. a_close(P,0);
  54.  
  55. textcolor(LIGHTGRAY);
  56. textbackground(BLACK);
  57. clrscr();
  58. exit(0);
  59. }
  60.  
  61.  
  62. void host_mode(void)
  63. {
  64. int x,y;
  65. char temp[80];
  66. struct tm *ptr;
  67. time_t lt;
  68.  
  69. TIMEOUT*=2;                                    /* wait longer if in host mode */
  70. gotoxy(29,1);
  71. cprintf(" H-SEND Host Mode V1.0 ");
  72. window(4,2,77,18);
  73.  
  74. for(;;){
  75.  bottom(&x,&y);
  76.  cprintf("Modem is currently OFF-LINE.  Press ESC to exit.");
  77.  a_printf(P,"AT S0=%d V0\n\r",RINGS);
  78.  do{
  79.     if(kbhit()) if(getch()==27){                    /* ESC key to exit       */
  80.         clrscr();
  81.         return;}
  82.     }while(OFFLINE);                            /* wait here for carrier */
  83.  
  84. cprintf("\n\rModem is currently ON-LINE");
  85. lt=time(NULL);
  86. ptr=localtime(<);
  87. strcpy(temp,"On-line: ");
  88. strcat(temp,asctime(ptr));
  89. strcat(temp,"\n\r");
  90. report(temp);
  91. top(x,y);
  92. if(password_check()) while(cmd_mode(1));
  93. init_modem();
  94. init_modem();                                    /* make sure modem is reset */
  95. }
  96. }
  97.  
  98.  
  99. int guest_mode(batchfile)
  100. char *batchfile;
  101. {
  102. FILE *fp;
  103. char str[100],temp[80],*buf;
  104. int i,x,y,flag=1;
  105. struct tm *ptr;
  106. time_t lt;
  107.  
  108. if(FLAGFILEFLAG) remove(FLAGFILE);
  109.  
  110. strcpy(str,"Executing: ");
  111. strcat(str,batchfile);
  112. gotoxy(39-strlen(batchfile),1);
  113. cprintf(" %s ",str);
  114. window(4,3,77,18);
  115.  
  116. AGAIN:
  117. if((fp=fopen(batchfile,"r"))==NULL){
  118.     report("ERROR- Unable to open batchfile");
  119.     return 0;}                                /* return error condition */
  120. do{
  121.     if(fgets(str,100,fp)){
  122.         for(i=0;i<strlen(str);i++)
  123.             if(str[i]=='\n' || str[i]=='\r') str[i]=NULL;
  124.     strupr(str);
  125.     textcolor(WHITE);
  126.     report("");
  127.     report(str);
  128.     textcolor(LIGHTCYAN);
  129.     flag=interpret_batch_cmd(str);}
  130. }while(!feof(fp) && !flag && !HARDERR_FLAG);
  131.  
  132. scb(EOC,0,NULL);
  133. lt=time(NULL);
  134. ptr=localtime(<);
  135. strcpy(temp,"Call terminated: ");
  136. strcat(temp,asctime(ptr));
  137. report(temp);
  138. fclose(fp);
  139. if(flag || HARDERR_FLAG){
  140.     report("ON-LINE transfer was not sucessful");
  141.     x=wherex(); y=wherey();
  142.     if((buf=malloc(2*44*9))==NULL) exit(1);
  143.     gettext(18,5,62,13,buf);
  144.     window(18,5,62,13);
  145.     textbackground(LIGHTCYAN);
  146.     textcolor(WHITE);
  147.     clrscr();
  148.     empty_box(2,1,44,9);
  149.     gotoxy(11,1);
  150.     cprintf(" Data Transfer Failure ");
  151.     gotoxy(5,4);
  152.     cprintf("The transfer has not been completed.");
  153.         beep(1,300);
  154.     beep(1,1000);
  155.     do{
  156.         gotoxy(5,6);
  157.         cprintf("Would you like to try again? <Y or N>");
  158.         i=getch();
  159.         i=toupper(i);
  160.         if(i!='Y' && i!='N') beep(1,1000);
  161.         }while(i!='Y' && i!='N');
  162.     cprintf("%c",i);
  163.     textbackground(BLACK);
  164.     window(4,3,77,18);
  165.     puttext(18,5,62,13,buf);
  166.     gotoxy(x,y);
  167.     if(i=='Y') goto AGAIN;
  168.  
  169.     window(4,3,77,18);
  170.     puttext(18,5,62,13,buf);
  171.     return 0;}
  172.  
  173.  else{
  174.     report("No error occurred");
  175.     if(FLAGFILEFLAG) if((fp=fopen(FLAGFILE,"a"))==NULL){
  176.         if((fp=fopen(FLAGFILE,"r"))==NULL) report("Unable to create flagfile");}
  177.     return 1;}
  178.  
  179. }
  180.  
  181.  
  182. int cmd_mode(host_flag)                            /* return 0 at end of sesssion      */
  183. {
  184. unsigned char buf[518];
  185. int cmdreq;
  186.  
  187. for(;;){
  188.  if(host_flag){
  189.     if(!scb(OKTS,0,NULL)){                        /* OK to send                       */
  190.         report("ERROR- OKTS transmission not acknowledged");
  191.         return 0;}}
  192.  
  193.  
  194.  do{                                            /* receiving command                */
  195.     if(OFFLINE){
  196.         report("DATA CONNECTION LOST");
  197.         return 0;}
  198.     cmdreq=rcb(0,buf);
  199.     }while(!cmdreq);
  200.  
  201. switch(cmdreq){
  202.  
  203.     case OKTS: return -1;
  204.  
  205.     case SFR: send_file_request(buf,0);
  206.             break;
  207.  
  208.     case IFR: create_local_file(buf);
  209.             break;
  210.  
  211.     case DF:  delete_files(buf,HOST);
  212.             break;
  213.  
  214.     case EM:  error_message(buf);
  215.             break;
  216.  
  217.     case EOC: report("Transmission terminated normally");
  218.             return 0;
  219.  
  220.     case RESTART: report("Restarting hsend...");
  221.                 execlp("hsend.exe",NULL,NULL);
  222.  
  223.     default:  report("cmd_mode() default");
  224.             if(!host_flag) return -1;}            /* switch back to active mode */
  225.  
  226.  
  227. } /* endless loop */
  228. }
  229.  
  230.  
  231. int passive(void)                                /* switch to passive mode */
  232. {
  233. int status;
  234.  
  235. for(;;){
  236.     status=cmd_mode(0);                            /* will be executing received commands while here */
  237.  
  238.     if(!status) return 1;                        /* outgoing command not acknowledged, returning 1 will
  239.                                                cause guest_mode() to exit with an error condition  */
  240.  
  241.     if(status==-1) return 0;                        /* OKTS received, switch back to send mode        */
  242.  
  243. } /* endless loop */
  244. }
  245.  
  246.  
  247. int interpret_batch_cmd(cmd)
  248. char *cmd;
  249. {
  250. char temp[512],from[100],to[100];
  251. int i;
  252.  
  253. if(strlen(cmd)<4) return 0;                        /* ignore trash */
  254.  
  255. if(!strncmp(cmd,"CALL",4)) return(call_remote(cmd));
  256.  
  257. if(!strncmp(cmd,"REM",3)) return 0;                /* remarks */
  258.  
  259. if(scb(EM,2," ")) if(!rcb(OKTS,temp)) rcb(OKTS,temp);    /* send a blank line */
  260. if(scb(EM,strlen(cmd)+1,cmd)) if(!rcb(OKTS,temp)) rcb(OKTS,temp); /* send the command  */
  261.  
  262. /*----------------------- COPY FROM HOST TO GUEST --------------------------------*/
  263. if(!strncmp(cmd,"COPY H",6)){
  264.     parse(cmd);
  265.     if(!seperate(cmd,from,to,' ')){
  266.         report("ERROR- COPY command syntax");
  267.         return 1;}
  268.     for(i=0;i<strlen(from);i++)                    /* remove H and L designations */
  269.         from[i]=from[i+1];
  270.     for(i=0;i<strlen(to);i++)
  271.         to[i]=to[i+1];
  272.  
  273.     strcpy(temp,from);                            /* set up SFR string */
  274.     strcat(temp,"|");
  275.     strcat(temp,to);
  276.  
  277.     if(!scb(SFR,strlen(temp)+1,temp)){
  278.         report("ERROR- Host unable to verify SFR transmission");
  279.         return 1;}
  280.      else return passive();}
  281. /*--------------------------------------------------------------------------------*/
  282.  
  283. /*----------------------- COPY FROM GUEST TO HOST --------------------------------*/
  284. if(!strncmp(cmd,"COPY L",6)){
  285.     parse(cmd);
  286.     if(!seperate(cmd,from,to,' ')){
  287.         report("ERROR- COPY command syntax");
  288.         return 1;}
  289.     for(i=0;i<strlen(from);i++)                    /* remove H and L designations */
  290.         from[i]=from[i+1];
  291.     for(i=0;i<strlen(to);i++)
  292.         to[i]=to[i+1];
  293.  
  294.     strcpy(temp,from);                            /* set up SFR string */
  295.     strcat(temp,"|");
  296.     strcat(temp,to);
  297.  
  298.     return send_file_request(temp,1);}
  299. /*--------------------------------------------------------------------------------*/
  300.  
  301.  
  302. /*-------------------------DELETE FILES-------------------------------------------*/
  303. if(!strncmp(cmd,"DELETE",6)){
  304.     parse(cmd);
  305.     if(cmd[0]=='H'){
  306.         for(i=0;i<strlen(cmd);i++)
  307.             cmd[i]=cmd[i+1];                    /* remove the first character (H) */
  308.         {if(!scb(DF,strlen(cmd)+1,cmd)){
  309.             report("ERROR- Host unable to verify DF transmission");
  310.             return 1;}
  311.          else return passive();}}
  312.      if(cmd[0]=='L'){
  313.           for(i=0;i<strlen(cmd);i++)
  314.             cmd[i]=cmd[i+1];                    /* remove the first character (H) */
  315.         delete_files(cmd,GUEST);
  316.         return 0;}}
  317. /*--------------------------------------------------------------------------------*/
  318.  
  319. report("ERROR- Command syntax");
  320. return 1;
  321. }
  322.  
  323.  
  324. void report(message)
  325. char *message;
  326. {
  327.  
  328. FILE *fp;
  329.  
  330. if(SCREENFLAG) cprintf("%s\n\r",message);
  331.  
  332. if(!LOGFILEFLAG) return;
  333. if((fp=fopen(LOGFILE,"a"))==NULL){
  334.     if((fp=fopen(LOGFILE,"r"))==NULL) return;}
  335.  
  336. fprintf(fp,"%s\n\r",message);
  337. fclose(fp);
  338. }
  339.  
  340.  
  341. int init_modem(void)
  342. {
  343. int x;
  344.  
  345. if(P) a_close(P,0);
  346. P=a_open(COMM,BAUD,0,8,1,8000,2000);                /* open at no parity,8 dbits, 1 sbit 8K input ,2k output buffer */
  347. a_setmcr(P,MCR_DTR+MCR_RTS);                        /* set DTR and RTS high                    */
  348. a_iflush(P);                                    /* clear input buffer